import { Component, OnInit, OnDestroy } from '@angular/core';
import { Subscription } from 'rxjs';
import { DashShopComponentService } from './shop.component.service';

import { AppSettings } from "../../../../app.settings";

import { Util } from '../../../../utils/util';

import { Scope } from '../../../../utils/emit';

@Component({
  selector: 'dashborad-shop',
  templateUrl: './shop.component.html',
  providers: [DashShopComponentService]
})
export class DashboradShop {


  busy: Subscription;

  shops: Array<any> = [];

  sellEvent: Subscription;

  p =1;
  // date
  vm = {
    date: ''
  };

  // 类目 接收pie component cat change
  cat: any = {
    catName: "",
    wholeName: "",
    categoryId: ""
  };

  // 参数
  param: any = {
    "rows": 15,
    "start": 0,
    "categoryId": "",
    "brandName": '""',
    "sort": 'sellMoney',
    "sortType": 'desc'
  };

  // 列名
  public columns: Array<any> = [
    {
      title: '店铺名',
      name: 'shopName',
      sort: false
    },
    {
      title: '日销售额',
      name: 'sellMoney',
      sort: true
    },
    {
      title: '日销售量',
      name: 'sellAmount',
      sort: true
    },
    {
      title: '近30天销售额',
      name: 'monthSellMoney',
      sort: true
    },
    {
      title: '近30天销售量',
      name: 'monthSellAmount',
      sort: true
    }
  ];


  constructor(private _service: DashShopComponentService, private appSettings: AppSettings, private util: Util, private scope: Scope) { }

  currentValue: any;
  ngOnInit() {

    // 如果不是第一次进入
    this.cat = this.appSettings.getAppCat();

    if (this.cat.categoryId != '') {
      this.http(this.param);
    }

    this.sellEvent = this.scope.on('dashborad-all-cur-cat', (data) => {

      console.log("dash shop");
      console.log(data);

      this.cat = data.data;

      this.http(this.param);

    });


  }


  ngOnDestroy() {

    if (this.busy) {
      this.busy.unsubscribe();
    }
    if (this.sellEvent) {
      this.sellEvent.unsubscribe();
    }

  }

  // st-sort directive emit
  sortChanged(sort) {
    console.log('xxx');
    console.log(sort);
    this.http(this.param);
  }


  export() {

    console.log("export");
    let dim = ['date', 'categoryId', 'categoryName', 'categoryLevel', 'shopName', 'uid', 'shopType', 'sellMoney', 'sellAmount', 'monthSellMoney', 'monthSellAmount'];

    let dimText = ['时间', '类目id', '类目', '类目等级', '店铺名', '店铺id', '店铺类型', '日销售额', '日销售量', '近30天销售额', '近30天销售量'];

    let data = this.util.jsonToArrry(this.shops, dim, dimText);


    this.util.export(data, this.cat.catName + "类目店铺排行榜");
  }

  http(param) {

    this.shops = [];

    this.shops.length = 0;

    this.param.categoryId = this.cat.categoryId;

    var res = {"code":0,"msg":"成功","data":{"value":[{"date":"2017-12-03 00:00:00","categoryId":"50019780","categoryName":"数码/手机/家电--平板电脑/MID","categoryLevel":2,"brandName":"","shopName":"旺隆数码通讯","uid":"446381428","shopType":"C店","sellAmount":213,"sellMoney":511223.2,"monthSellAmount":6107,"monthSellMoney":1.46371396E7},{"date":"2017-12-03 00:00:00","categoryId":"50019780","categoryName":"数码/手机/家电--平板电脑/MID","categoryLevel":2,"brandName":"","shopName":"苏宁易购官方旗舰店","uid":"2616970884","shopType":"商城","sellAmount":202,"sellMoney":477001.0,"monthSellAmount":32478,"monthSellMoney":8.2173302E7},{"date":"2017-12-03 00:00:00","categoryId":"50019780","categoryName":"数码/手机/家电--平板电脑/MID","categoryLevel":2,"brandName":"","shopName":"三际数码官方旗舰店","uid":"268451883","shopType":"商城","sellAmount":192,"sellMoney":458006.0,"monthSellAmount":7286,"monthSellMoney":1.555601E7},{"date":"2017-12-03 00:00:00","categoryId":"50019780","categoryName":"数码/手机/家电--平板电脑/MID","categoryLevel":2,"brandName":"","shopName":"卓辰数码旗舰店","uid":"1669409267","shopType":"商城","sellAmount":159,"sellMoney":414524.0,"monthSellAmount":10944,"monthSellMoney":3.0952623E7},{"date":"2017-12-03 00:00:00","categoryId":"50019780","categoryName":"数码/手机/家电--平板电脑/MID","categoryLevel":2,"brandName":"","shopName":"jumper中柏旗舰店","uid":"1062583331","shopType":"商城","sellAmount":198,"sellMoney":263022.0,"monthSellAmount":2622,"monthSellMoney":2895358.0},{"date":"2017-12-03 00:00:00","categoryId":"50019780","categoryName":"数码/手机/家电--平板电脑/MID","categoryLevel":2,"brandName":"","shopName":"我就爱购数码","uid":"277891388","shopType":"C店","sellAmount":87,"sellMoney":227126.0,"monthSellAmount":2590,"monthSellMoney":6743320.0},{"date":"2017-12-03 00:00:00","categoryId":"50019780","categoryName":"数码/手机/家电--平板电脑/MID","categoryLevel":2,"brandName":"","shopName":"微软一店","uid":"371545459","shopType":"C店","sellAmount":47,"sellMoney":194536.0,"monthSellAmount":480,"monthSellMoney":1980940.0},{"date":"2017-12-03 00:00:00","categoryId":"50019780","categoryName":"数码/手机/家电--平板电脑/MID","categoryLevel":2,"brandName":"","shopName":"华为官方旗舰店","uid":"2838892713","shopType":"商城","sellAmount":119,"sellMoney":188693.0,"monthSellAmount":8313,"monthSellMoney":1.3108893E7},{"date":"2017-12-03 00:00:00","categoryId":"50019780","categoryName":"数码/手机/家电--平板电脑/MID","categoryLevel":2,"brandName":"","shopName":"拓普数码","uid":"126195000","shopType":"全球购","sellAmount":80,"sellMoney":180640.0,"monthSellAmount":1480,"monthSellMoney":3345290.0},{"date":"2017-12-03 00:00:00","categoryId":"50019780","categoryName":"数码/手机/家电--平板电脑/MID","categoryLevel":2,"brandName":"","shopName":"天泽数码专营店","uid":"676655912","shopType":"商城","sellAmount":378,"sellMoney":169004.0,"monthSellAmount":11079,"monthSellMoney":5007062.0},{"date":"2017-12-03 00:00:00","categoryId":"50019780","categoryName":"数码/手机/家电--平板电脑/MID","categoryLevel":2,"brandName":"","shopName":"乐购数码生活馆","uid":"95807881","shopType":"C店","sellAmount":36,"sellMoney":157968.0,"monthSellAmount":510,"monthSellMoney":2251180.0},{"date":"2017-12-03 00:00:00","categoryId":"50019780","categoryName":"数码/手机/家电--平板电脑/MID","categoryLevel":2,"brandName":"","shopName":"盛世奕想数码专营店","uid":"202216032","shopType":"商城","sellAmount":406,"sellMoney":157468.0,"monthSellAmount":10812,"monthSellMoney":4542896.0},{"date":"2017-12-03 00:00:00","categoryId":"50019780","categoryName":"数码/手机/家电--平板电脑/MID","categoryLevel":2,"brandName":"","shopName":"华为卓瑞尔专卖店","uid":"2711014365","shopType":"商城","sellAmount":93,"sellMoney":156141.0,"monthSellAmount":6777,"monthSellMoney":1.1389954E7},{"date":"2017-12-03 00:00:00","categoryId":"50019780","categoryName":"数码/手机/家电--平板电脑/MID","categoryLevel":2,"brandName":"","shopName":"荣耀官方旗舰店","uid":"1114511827","shopType":"商城","sellAmount":169,"sellMoney":142880.0,"monthSellAmount":11058,"monthSellMoney":1.010802E7},{"date":"2017-12-03 00:00:00","categoryId":"50019780","categoryName":"数码/手机/家电--平板电脑/MID","categoryLevel":2,"brandName":"","shopName":"绿森数码官方旗舰店","uid":"197232874","shopType":"商城","sellAmount":41,"sellMoney":117384.0,"monthSellAmount":2831,"monthSellMoney":8040569.0}],"count":713}};

      // console.log(res);

      this.shops = res.data.value;

      // console.log(this.shops);

      this.vm.date = this.shops[0].date;


    
  }




}
